﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using SensitivityTest.model;
using System.Net;
using System.Net.Sockets;
namespace SensitivityTest
{
    public partial class Frm_Config_update : Form
    {
        public Frm_Config_update()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 罗列已有产品配置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Frm_Config_update_Load(object sender, EventArgs e)
        {
            GetLocalIP();
            DataTable dt = SQLData.SelectConfigData("select distinct ProductTypeID  from Test_OIP3_Config order by ProductTypeID");
            cmb_ProductName.DataSource = dt;
            cmb_ProductName.DisplayMember = "ProductTypeID";
            cmb_ProductName.ValueMember = "ProductTypeID";
        }
        /// <summary>
        /// 产品名称对应版本号
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cmb_ProductName_SelectedIndexChanged(object sender, EventArgs e)
        {
            DataTable dt = SQLData.SelectConfigData("select Versions from Test_OIP3_Config where ProductTypeID='" + cmb_ProductName.Text.Trim() + "'");
            cmb_Versions.DataSource = dt;
            cmb_Versions.DisplayMember = "Versions";
            cmb_Versions.ValueMember = "Versions";
        }
        /// <summary>
        /// 各产品不同版本对应的具体配置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cmb_Versions_SelectedIndexChanged(object sender, EventArgs e)
        {
            DataTable dt = SQLData.SelectConfigData("select * from Test_OIP3_Config where ProductTypeID='" + cmb_ProductName.Text.Trim() + "' and Versions ='" + cmb_Versions.Text.Trim() + "'");
            txt_sig1Freq.Text = (string)dt.Rows[0]["F1"];
            txt_sig2Freq.Text = (string)dt.Rows[0]["F2"];
            txt_centerFreq.Text = (string)dt.Rows[0]["CenterFreq"];
            txt_span.Text=(string)dt.Rows[0]["SPAN"];
            txt_RBW.Text=(string)dt.Rows[0]["RBW"];
            txt_VBW.Text = (string)dt.Rows[0]["VBW"];
            txt_REF.Text=(string)dt.Rows[0]["REF"];
            txt_Atten.Text=(string)dt.Rows[0]["Atten"];
            txt_spec.Text = (string)dt.Rows[0]["Spec"];
            txt_ChanList.Text=(string)dt.Rows[0]["ChanList"];
            if ((Boolean)dt.Rows[0]["Enable"])
            {
                txt_Enable.Text = "1";
            }
            else
            {
                txt_Enable.Text = "0";
            }
        }

        private void btn_reset_Click(object sender, EventArgs e)
        {
            clearControlData();
        }

        private void clearControlData()
        {
            cmb_ProductName.Text = "";
            txt_sig1Freq.Text = "";
            txt_sig2Freq.Text = "";
            txt_centerFreq.Text = "";
            txt_span.Text = "";
            txt_RBW.Text = "";
            txt_VBW.Text = "";
            txt_REF.Text = "";
            txt_Atten.Text = "";
            cmb_Versions.Text = "";
            txt_Enable.Text = "";
            txt_spec.Text = "";
        }

        private void Frm_Config_update_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.DialogResult = DialogResult.OK;
        }
        /// <summary>
        /// 修改配置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_updateConfig_Click(object sender, EventArgs e)
        {
            try
            {
                SQLData.UpdateConfigData("update Test_OIP3_Config set F1='" + txt_sig1Freq.Text.Trim() + "',F2='" + txt_sig2Freq.Text.Trim() + "',CenterFreq='" + txt_centerFreq.Text.Trim() + "',SPAN='" + txt_span.Text.Trim() + "',RBW='" + txt_RBW.Text.Trim() + "',VBW='" + txt_VBW.Text.Trim() + "',REF='" + txt_REF.Text.Trim() + "',Atten='" + txt_Atten.Text.Trim() + "',Enable='" + txt_Enable.Text.Trim() + "',Spec='"+ txt_spec.Text.Trim() +"' where ProductTypeID='" + cmb_ProductName.Text.Trim() + "' and Versions='" + cmb_Versions.Text.Trim() + "' ");
                MessageBox.Show("修改成功");
                this.Close();
            }
            catch
            {
                MessageBox.Show("修改失败");
            }
        }

        private void GetLocalIP()
        {
            string NetSeg =MDIParent1.localIP.Split('.')[2];
            if (int.Parse(NetSeg) >= 80 && int.Parse(NetSeg) <= 89)
            {
                SQLData.mServer = "10.53.80.11";
            }
            else if (int.Parse(NetSeg) >= 60 && int.Parse(NetSeg) <= 69)
            {
                SQLData.mServer = "10.53.60.6";
            }
            else
            {
                SQLData.mServer = "10.53.20.170";
            }
        }
    }
}
